home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 3 / Light ROM 3 - Disc 2.iso / programs / pc / l_parser / dxf2lw.c < prev    next >
C/C++ Source or Header  |  1995-03-23  |  18KB  |  596 lines

  1. /*
  2.     DXFTOLW.C    
  3.     Copyright (C) 1994  Earl C. Terwilliger
  4.             158 Eades Drive
  5.             Irvine, KY 40336
  6.             Phone (606)-723-5718
  7.             Internet: aisterwi@acs.eku.edu
  8. */
  9. #define LINESIZE 256
  10. #define DXF_VERTICES 4
  11. #define MAX_SAVE 10
  12. #define MAX_POINTS 65532
  13. #define CR 0x0A
  14. #define LF 0x0D
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include <fcntl.h>
  19. #include <io.h>
  20. #include <sys\types.h>
  21. #include <sys\stat.h>
  22. FILE *fp1;
  23. int fp2, fp3;
  24. static unsigned char buf[LINESIZE],buf1[LINESIZE];
  25. static unsigned int poly=0, color=0, layer=0;
  26. static int vertices = -1;
  27. static float prev_xyz[DXF_VERTICES*MAX_SAVE][3];
  28. static unsigned long prev_index[DXF_VERTICES*MAX_SAVE];
  29. float polygon[DXF_VERTICES][3];
  30. static unsigned long polygons = 0l, totpoints = 0l;
  31. static unsigned long triangles= 0l, twoptpols = 0l;
  32. static unsigned long file_size = 0l;
  33. static unsigned int surfaces[8][2];
  34. static unsigned int totsurfaces = 0;
  35. static unsigned int xyccpoly=0,yzccpoly=0,xzccpoly=0;
  36. static unsigned int xycpoly=0,yzcpoly=0,xzcpoly=0;
  37. static unsigned int xypols=0,yzpols=0,xzpols=0,nonpols=0;
  38. static double area;
  39.  
  40. #pragma pack(1)
  41.  
  42. static struct  {
  43.     char form_hdr[4];
  44.     long form_len;
  45.     char form_lwob[4];
  46. } FORM = {  { 'F','O','R','M'}, 0L, { 'L','W','O','B'} }; 
  47.  
  48. static struct  {
  49.     char layr_hdr[4];
  50.     long layr_len;
  51.     int  layr_id;
  52.     int  layr_nbr;
  53.     char layr_name[8];
  54. } LAYR = {  { 'L','A','Y','R'}, 
  55.           12L, 0x0100, 0x0000, 
  56.         { 'n','o','n','a','m','e','\0','\0' } }; 
  57.  
  58. static struct  {
  59.     char pnts_hdr[4];
  60.     long pnts_len;
  61.     float xyz[3];
  62. } PNTS = {  { 'P','N','T','S'}, 0l, 0l, 0l, 0l }; 
  63.  
  64. static struct  {
  65.     unsigned int layer;
  66.     unsigned int vertices;
  67.     unsigned int color;
  68.     unsigned long index[DXF_VERTICES];
  69.     unsigned int unique;
  70.     float xyz[DXF_VERTICES][3];
  71. } POLY;
  72.  
  73. static struct  {
  74.     char pols_hdr[4];
  75.     long pols_len;
  76.     unsigned int pols_points[6];
  77. } POLS = {  { 'P','O','L','S'}, 0L, { 4,0,0,0,0,1 }  }; 
  78.  
  79. static struct  {
  80.     char srfs_hdr[4];
  81.     long srfs_len;
  82.     char srfs[8][6];
  83. } SRFS = {
  84.       { 'S', 'R', 'F', 'S' } , 48l,  
  85.       { { 'P', 'E', 'N', '0', '\0', '\0' },
  86.     { 'P', 'E', 'N', '1', '\0', '\0' },
  87.     { 'P', 'E', 'N', '2', '\0', '\0' },
  88.     { 'P', 'E', 'N', '3', '\0', '\0' },
  89.     { 'P', 'E', 'N', '4', '\0', '\0' },
  90.     { 'P', 'E', 'N', '5', '\0', '\0' },
  91.     { 'P', 'E', 'N', '6', '\0', '\0' },
  92.     { 'P', 'E', 'N', '7', '\0', '\0' } }
  93. };
  94.  
  95. static struct  {
  96.     char surf_hdr[4];
  97.     char surf_len[4];
  98.     char surf_name[6];
  99.     char surf_color[4];
  100.     char surf_color_len[2];
  101.     char surf_color_rgb1[4];
  102.     char surf_flag[4];
  103.     char surf_flag_len[2];
  104.     char surf_flag_flags[2];
  105.     char surf_diff[4];
  106.     char surf_diff_len[2];
  107.     char surf_diff_diff[2];
  108. } SURF[8] = {
  109.     
  110.     {{ 'S', 'U', 'R', 'F' }, { 0x00, 0x00, 0x00, 0x20 },
  111.     { 'P', 'E', 'N', '0', '\0', '\0' },
  112.     { 'C', 'O', 'L', 'R' }, { 0x00, 0x04 },  { '\0', '\0', '\0', '\0' },
  113.     { 'F', 'L', 'A', 'G' }, { 0x00, 0x02 },  { 0x00, 0x04 }, 
  114.     { 'D', 'I', 'F', 'F' }, { 0x00, 0x02 },  { 0x00, 0xFF } },
  115.  
  116.     {{ 'S', 'U', 'R', 'F' }, { 0x00, 0x00, 0x00, 0x20 },
  117.     { 'P', 'E', 'N', '1', '\0', '\0' },
  118.     { 'C', 'O', 'L', 'R' }, { 0x00, 0x04 },  { 0xFF, '\0', '\0', '\0' },
  119.     { 'F', 'L', 'A', 'G' }, { 0x00, 0x02 },  { 0x00, 0x04 }, 
  120.     { 'D', 'I', 'F', 'F' }, { 0x00, 0x02 },  { 0x00, 0xFF } },
  121.     
  122.     { { 'S', 'U', 'R', 'F' }, { 0x00, 0x00, 0x00, 0x20 },
  123.     { 'P', 'E', 'N', '2', '\0', '\0' },
  124.     { 'C', 'O', 'L', 'R' }, { 0x00, 0x04 },  { 0xFF, 0xFF, '\0', '\0' },
  125.     { 'F', 'L', 'A', 'G' }, { 0x00, 0x02 },  { 0x00, 0x04 }, 
  126.     { 'D', 'I', 'F', 'F' }, { 0x00, 0x02 },  { 0x00, 0xFF } }, 
  127.     
  128.     { { 'S', 'U', 'R', 'F' }, { 0x00, 0x00, 0x00, 0x20 },
  129.     { 'P', 'E', 'N', '3', '\0', '\0' },
  130.     { 'C', 'O', 'L', 'R' }, { 0x00, 0x04 },  { '\0', 0xFF, '\0', '\0' },
  131.     { 'F', 'L', 'A', 'G' }, { 0x00, 0x02 },  { 0x00, 0x04 }, 
  132.     { 'D', 'I', 'F', 'F' }, { 0x00, 0x02 },  { 0x00, 0xFF } }, 
  133.     
  134.     {{ 'S', 'U', 'R', 'F' }, { 0x00, 0x00, 0x00, 0x20 },
  135.     { 'P', 'E', 'N', '4', '\0', '\0' },
  136.     { 'C', 'O', 'L', 'R' }, { 0x00, 0x04 },  { '\0', 0xFF, 0xFF, '\0' },
  137.     { 'F', 'L', 'A', 'G' }, { 0x00, 0x02 },  { 0x00, 0x04 }, 
  138.     { 'D', 'I', 'F', 'F' }, { 0x00, 0x02 },  { 0x00, 0xFF } }, 
  139.     
  140.     {{ 'S', 'U', 'R', 'F' }, { 0x00, 0x00, 0x00, 0x20 },
  141.     { 'P', 'E', 'N', '5', '\0', '\0' },
  142.     { 'C', 'O', 'L', 'R' }, { 0x00, 0x04 },  { '\0', '\0', 0xFF, '\0' },
  143.     { 'F', 'L', 'A', 'G' }, { 0x00, 0x02 },  { 0x00, 0x04 }, 
  144.     { 'D', 'I', 'F', 'F' }, { 0x00, 0x02 },  { 0x00, 0xFF } }, 
  145.     
  146.     {{ 'S', 'U', 'R', 'F' }, { 0x00, 0x00, 0x00, 0x20 },
  147.     { 'P', 'E', 'N', '6', '\0', '\0' },
  148.     { 'C', 'O', 'L', 'R' }, { 0x00, 0x04 },  { 0xFF, '\0', 0xFF, '\0' },
  149.     { 'F', 'L', 'A', 'G' }, { 0x00, 0x02 },  { 0x00, 0x04 }, 
  150.     { 'D', 'I', 'F', 'F' }, { 0x00, 0x02 },  { 0x00, 0xFF } }, 
  151.     
  152.     {{ 'S', 'U', 'R', 'F' }, { 0x00, 0x00, 0x00, 0x20 },
  153.     { 'P', 'E', 'N', '7', '\0', '\0' },
  154.     { 'C', 'O', 'L', 'R' }, { 0x00, 0x04 },  { 0xFF, 0xFF, 0xFF, '\0' },
  155.     { 'F', 'L', 'A', 'G' }, { 0x00, 0x02 },  { 0x00, 0x04 }, 
  156.     { 'D', 'I', 'F', 'F' }, { 0x00, 0x02 },  { 0x00, 0xFF } } };
  157.  
  158. main(argc,argv)
  159.     int argc;
  160.     char *argv[];
  161. {
  162.     unsigned char rawfile[64], infile[64], outfile[64];
  163.     unsigned int c,d,e;
  164.     float t;
  165.     int code,debug=0;
  166.     if (argc < 2) syntax(argv[0]);
  167.     strcpy(infile,argv[1]); 
  168.     if (!strchr(infile,'.')) strcat(infile,".DXF");
  169.     if (!(fp1 = fopen(infile,"rb"))) {
  170.     printf("Cannot open input file %s!\n",infile);
  171.     exit(1);
  172.     }
  173.     if ((argv[argc-1][0] == '-') && (toupper(argv[argc-1][1]) == 'D')) { 
  174.     debug = 1;
  175.     --argc;
  176.     }
  177.     if (argc < 3) {
  178.     strcpy(outfile,infile);
  179.     strcpy(strchr(outfile,'.'),".OBJ");
  180.     }
  181.     else {
  182.     strcpy(outfile,argv[2]);
  183.     if (!strchr(infile,'.')) strcat(infile,".OBJ");
  184.     }
  185.     strcpy(rawfile,infile);
  186.     strcpy(strchr(rawfile,'.'),".RAW");
  187.     if (!(fp2 = open(outfile,O_BINARY|O_CREAT|O_WRONLY|O_TRUNC))) {
  188.     printf("Cannot open output file %s!\n",outfile);
  189.     fclose(fp1);
  190.     exit(1);
  191.     }
  192.     if (!(fp3 = open(rawfile,O_BINARY|O_CREAT|O_WRONLY|O_TRUNC))) {
  193.     printf("Cannot open temporary output file %s!\n",rawfile);
  194.     fclose(fp1);
  195.     close(fp2);
  196.     exit(1);
  197.     }
  198.     printf("\n%s Started\n",argv[0]);
  199.     printf("Reading DXF File    - Creating RAW  file  ....");
  200.     totpoints = polygons = 0l;
  201.     while(getline(fp1,buf)) {
  202.     if(!getline(fp1,buf1)) {
  203.         printf("\nError - Group Code %s with no data\n",buf);
  204.         fclose(fp1);
  205.         close(fp2);
  206.         close(fp3);
  207.         unlink(outfile);
  208.         unlink(rawfile);
  209.         break;
  210.     }
  211.     code = atoi(buf);
  212.     switch (code) {
  213.         case 0:
  214.         if (poly) {  
  215.             lwout(vertices); 
  216.             vertices = -1; 
  217.             color = layer = 0;
  218.             poly = 0x00; 
  219.         }
  220.         if (strstr(buf1,"3DFACE")) {  poly = 0x80; ++polygons; }
  221.         break;
  222.         case   8:
  223.         layer = atoi(buf1);
  224.         break;
  225.         case  10:
  226.         case  11:
  227.         case  12:
  228.         case  13:
  229.         ++vertices;
  230.         polygon[vertices][0] = atof(buf1);
  231.         break;
  232.         case  20:
  233.         case  21:
  234.         case  22:
  235.         case  23:
  236.         polygon[vertices][1] = atof(buf1);
  237.         break;
  238.         case  30:
  239.         case  31:
  240.         case  32:
  241.         case  33:
  242.         polygon[vertices][2] = atof(buf1);
  243.         break;
  244.         case  62:
  245.         color = atoi(buf1);
  246.         break;
  247.         case 999:
  248.         break;
  249.         default :
  250.         break;
  251.     }
  252.     }
  253.     if (poly) lwout(vertices);
  254.     for(c=0;c<8;++c) { 
  255.     if (surfaces[c][0] == 1) { 
  256.         ++totsurfaces;
  257.         surfaces[c][1] = totsurfaces;
  258.     }
  259.     }
  260.     printf("\nPoints    %ld [unique]\nPolygons  %ld ",totpoints,polygons);
  261.     printf("[%ld triangles]  [%ld 2-point polygons]\n",triangles,twoptpols);
  262.     printf("Surfaces  %u\n",totsurfaces);
  263.     fclose(fp1);
  264.     close(fp3);
  265.     if (totpoints > MAX_POINTS) {
  266.     printf("\nERROR - maximum points exceeded.\nThe limit is %u points.\n",MAX_POINTS);
  267.     close(fp2);
  268.     unlink(outfile);
  269.     unlink(rawfile);
  270.     exit(99);                 
  271.     }
  272.     file_size  = 4l; 
  273.     file_size += 8l; 
  274.     file_size += totpoints * 12l;
  275.     file_size += 8l + (long)totsurfaces*6l; 
  276.     file_size += 8l; 
  277.     file_size += (polygons * 12l) - (triangles * 2l) - (twoptpols * 4l);
  278.     file_size += (long)totsurfaces*40l;
  279.     printf("Creating %s file size %ld\n",outfile,file_size+8l);
  280.     FORM.form_len = file_size;
  281.     rev_long((char *)&FORM.form_len);
  282.     write(fp2,(char *)&FORM,sizeof(FORM));
  283.     if (!(fp3 = open(rawfile,O_BINARY|O_RDONLY))) {
  284.     printf("Cannot open temporary output file %s!\n",rawfile);
  285.     close(fp2);
  286.     exit(1);
  287.     }
  288.     printf("\nRAW file  PASS 1    - Creating PNTS chunk ....");
  289.     PNTS.pnts_len = (long)totpoints*12l;
  290.     rev_long((char *)&PNTS.pnts_len);
  291.     write(fp2,(char *)&PNTS,8);
  292.     totpoints = polygons = 0;
  293.     while(read(fp3,&POLY,sizeof(POLY))) {
  294.     polygons += 1;
  295.     for(c=0;c<DXF_VERTICES;++c) {
  296.         if (!(POLY.unique & (0x01<<c))) continue;
  297.         totpoints += 1;
  298.         t = POLY.xyz[c][1];
  299.         POLY.xyz[c][1] = POLY.xyz[c][2];
  300.         POLY.xyz[c][2] = t;
  301.         for(d=0;d<3;++d) rev_long(&POLY.xyz[c][d]);
  302.         write(fp2,(char *)&POLY.xyz[c],12);
  303.     }
  304.     }
  305.     close(fp3);
  306.     printf("\nPoints    %ld [unique]    Polygons  %ld\n",totpoints,polygons);
  307.     printf("\nWriting to output   - Creating SRFS chunk ....");
  308.     printf("\nSurfaces  %u\n",totsurfaces);
  309.     SRFS.srfs_len = (long)totsurfaces*6l;
  310.     rev_long((char *)&SRFS.srfs_len);
  311.     write(fp2,(char *)&SRFS,8);
  312.     for(c=0;c<8;++c) if(surfaces[c][0] == 1) write(fp2,(char *)&SRFS.srfs[c],6);
  313.     if (!(fp3 = open(rawfile,O_BINARY|O_RDONLY))) {
  314.     printf("Cannot open temporary output file %s!\n",rawfile);
  315.     close(fp2);
  316.     exit(1);
  317.     }
  318.     printf("\nRAW file  PASS 2    - Creating POLS chunk ....");
  319.     POLS.pols_len = (polygons * 12l) - (triangles * 2l) - (twoptpols * 4l);
  320.     rev_long((char *)&POLS.pols_len);
  321.     write(fp2,(char *)&POLS,8);
  322.     totpoints = polygons = 0;
  323.     while(read(fp3,&POLY,sizeof(POLY))) {
  324.     poly_area_xy();
  325.     polygons  += 1;
  326.     totpoints += POLY.vertices;
  327.     POLS.pols_points[0] = POLY.vertices;
  328.     POLS.pols_points[POLY.vertices+1] =  surfaces[POLY.color][1];
  329.     for(e=0;e<(POLY.vertices+2);++e) rev_int((char *)&POLS.pols_points[e]);
  330.     write(fp2,(char *)POLS.pols_points,(4+(POLY.vertices*2)));
  331.     }
  332.     close(fp3);
  333.     printf("\nVertices  %ld    Polygons  %ld\n",totpoints,polygons);
  334.     printf("Front [xy]  Polygons  %2u  [%2u clockwise]  [%2u counterclockwise]\n",
  335.     xypols,xycpoly,xyccpoly);
  336.     printf("Side  [yz]  Polygons  %2u  [%2u clockwise]  [%2u counterclockwise]\n",
  337.     yzpols,yzcpoly,yzccpoly);
  338.     printf("Top   [xz]  Polygons  %2u  [%2u clockwise]  [%2u counterclockwise]\n",
  339.     xzpols,xzcpoly,xzccpoly);
  340.     printf("Non visible Polygons  %2u  [Includes 2-point polygons]\n",nonpols);
  341.     printf("\nInternal data used  - Creating SURF chunk ....");
  342.     for (c=0;c<8;++c) if(surfaces[c][0] == 1) write(fp2,(char *)&SURF[c],40);
  343.     close(fp2);
  344.     if (!debug) unlink(rawfile); 
  345.     printf("\nProgram complete.\n");
  346.     exit(0);
  347. }
  348. lwout(vertices)
  349.     unsigned int vertices;
  350. {
  351.     int c,d;
  352.     if (vertices < 0) return(0);
  353.     ++vertices;
  354.     if (vertices != DXF_VERTICES) {
  355.     printf("\nDXF file polygon vertices != %d\n",DXF_VERTICES);
  356.     exit(99);
  357.     }
  358.     while (color > 7) color -= 7;
  359.     surfaces[color][0] = 1;
  360.     POLY.vertices = vertices;
  361.     POLY.color    = color;
  362.     POLY.layer    = layer;
  363.     POLY.unique   = 0x00;
  364.     for(c=0;c<vertices;++c) {
  365.     for(d=0;d<3;++d) POLY.xyz[c][d] = polygon[c][d];
  366.     d = check_unique(POLY.xyz[c][0],POLY.xyz[c][1],POLY.xyz[c][2]);
  367.     if (d < DXF_VERTICES*MAX_SAVE) POLY.index[c] = prev_index[d];
  368.     else {
  369.         POLY.index[c] = totpoints;
  370.         POLY.unique |=  (0x01 << c);
  371.         totpoints += 1;
  372.         bubble_up(POLY.xyz[c][0],POLY.xyz[c][1],POLY.xyz[c][2],POLY.index[c]);
  373.     }
  374.     }
  375.     if ((POLY.xyz[2][0] == POLY.xyz[3][0]) &&
  376.     (POLY.xyz[2][1] == POLY.xyz[3][1]) &&
  377.     (POLY.xyz[2][2] == POLY.xyz[3][2])) { 
  378.     POLY.vertices -= 1; 
  379.     if ((POLY.xyz[2][0] == POLY.xyz[0][0]) &&
  380.         (POLY.xyz[2][1] == POLY.xyz[0][1]) &&
  381.         (POLY.xyz[2][2] == POLY.xyz[0][2])) { 
  382.         POLY.vertices -= 1;                  
  383.         twoptpols += 1;
  384.     }
  385.     else triangles += 1; 
  386.     }
  387.     write(fp3,(char *)&POLY,sizeof(POLY));
  388.     return(0);
  389. }
  390. check_unique(x,y,z) 
  391.     float x, y, z;
  392. {
  393.     int c;
  394.     if ((x == 0) && (y == 0) && (z == 0)) return(DXF_VERTICES*MAX_SAVE);
  395.     for (c=0;c<DXF_VERTICES*MAX_SAVE;++c) {
  396.     if ((x == prev_xyz[c][0]) &&
  397.         (y == prev_xyz[c][1]) &&
  398.         (z == prev_xyz[c][2]))  {
  399.         return(c);
  400.     }
  401.     }
  402.     return(c);
  403. }
  404. bubble_up(x,y,z,index)
  405.     float x,y,z;
  406.     unsigned long index;
  407. {
  408.     int c,d;
  409.     for (c=1;c<DXF_VERTICES*MAX_SAVE;++c) {
  410.     d = c - 1;
  411.     prev_xyz[d][0] = prev_xyz[c][0]; 
  412.     prev_xyz[d][1] = prev_xyz[c][1]; 
  413.     prev_xyz[d][2] = prev_xyz[c][2]; 
  414.     prev_index[d]  = prev_index[c];
  415.     }
  416.     d = c - 1;
  417.     prev_index[d]  = index;
  418.     prev_xyz[d][0] = x;
  419.     prev_xyz[d][1] = y;
  420.     prev_xyz[d][2] = z;
  421.     return(0);
  422. }
  423. getline(fp,buf)
  424.     FILE *fp;
  425.     unsigned char *buf;
  426. {
  427.     int c=0,d=0;
  428.     while((c=getc(fp)) != EOF) {
  429.     if (c == CR) continue;
  430.     if (c == LF) break;
  431.     ++d;
  432.     *buf++ = c;
  433.     }
  434.     *buf = 0;
  435.     return(d);
  436. }
  437. syntax(ptr)
  438.     char *ptr;
  439. {
  440.     printf("%s infile[.DXF] [outfile[.OBJ]] [-d]\n",ptr);
  441.     exit(1);
  442. }
  443. rev_long(buf)
  444.     unsigned char *buf;
  445. {
  446.     char bfr[2];
  447.     bfr[0] = buf[0];
  448.     bfr[1] = buf[1];
  449.     buf[0] = buf[3];
  450.     buf[1] = buf[2];
  451.     buf[2] = bfr[1];
  452.     buf[3] = bfr[0];
  453.     return(0);
  454. }
  455. rev_int(buf)
  456.     unsigned char *buf;
  457. {
  458.     char s;
  459.     s = buf[0];
  460.     buf[0] = buf[1];
  461.     buf[1] = s;
  462.     return(0);
  463. }
  464. /*
  465.  
  466.     area = 0.5 * ( ( x[0] * y[1] ) + ( x[1] * y[2] ) + ( x[2] * y[0] ) -
  467.            ( x[1] * y[0] ) - ( x[2] * y[1] ) - ( x[0] * y[2] ) );
  468.  
  469. and the area of a planar polygon is given by
  470.  
  471.     area = 0.0;
  472.     for ( i = 0; i < n - 1; i++ ) 
  473.     area += ( x[i] * y[i + 1] ) - ( x[i + 1] * y[i] );
  474.     area += ( x[n - 1] * y[0] ) - ( x[0] * y[n - 1] );
  475.     area /= 2.0;
  476.  
  477. If the area is a negative number, the polygon or triangle is clockwise, 
  478. if positive, it is counterclockwise. This is for the x-y plane of view. 
  479. If the polygon area is 0 then the polygon is not "viewed" from the x-y plane.
  480.  
  481. */
  482. poly_area_xy()
  483. {
  484.     int c=0,d=0;
  485.     if (POLY.vertices == 3) {
  486.       area = 0.5 * ( ( (double)POLY.xyz[0][0] * (double)POLY.xyz[1][2] ) + 
  487.              ( (double)POLY.xyz[1][0] * (double)POLY.xyz[2][2] ) + 
  488.              ( (double)POLY.xyz[2][0] * (double)POLY.xyz[0][2] ) - 
  489.              ( (double)POLY.xyz[1][0] * (double)POLY.xyz[0][2] ) - 
  490.              ( (double)POLY.xyz[2][0] * (double)POLY.xyz[1][2] ) - 
  491.              ( (double)POLY.xyz[0][0] * (double)POLY.xyz[2][2] ) );
  492.     } 
  493.     else {
  494.       area = 0.0;
  495.       for (c=0;c<POLY.vertices-1;++c)
  496.       area += ( (double)POLY.xyz[c][0]   * (double)POLY.xyz[c+1][2] ) - 
  497.           ( (double)POLY.xyz[c+1][0] * (double)POLY.xyz[c][2] );
  498.       area += ( (double)POLY.xyz[POLY.vertices-1][0] * (double)POLY.xyz[0][2] ) - 
  499.           ( (double)POLY.xyz[0][0] * (double)POLY.xyz[POLY.vertices-1][2] );
  500.       area /= 2.0;
  501.     }
  502.     if (area < 0.0) { 
  503.     ++xycpoly; 
  504.     ++xypols; 
  505.     for(c=0;c<POLY.vertices;++c) POLS.pols_points[c+1] =  POLY.index[c];
  506.     return(0);
  507.     }
  508.     if (area > 0.0) {
  509.     ++xyccpoly;
  510.     ++xypols;
  511.     for(c=0,d=POLY.vertices;c<POLY.vertices;++c,--d) {
  512.         POLS.pols_points[d] = POLY.index[c];
  513.     }
  514.     return(0);
  515.     }
  516.     if (area == 0.0)  poly_area_yz();
  517.     return(0);
  518. }
  519. poly_area_yz()
  520. {
  521.     int c=0,d=0;
  522.     if (POLY.vertices == 3) {
  523.       area = 0.5 * ( ( (double)POLY.xyz[0][1] * (double)POLY.xyz[1][2] ) + 
  524.              ( (double)POLY.xyz[1][1] * (double)POLY.xyz[2][2] ) + 
  525.              ( (double)POLY.xyz[2][1] * (double)POLY.xyz[0][2] ) - 
  526.              ( (double)POLY.xyz[1][1] * (double)POLY.xyz[0][2] ) - 
  527.              ( (double)POLY.xyz[2][1] * (double)POLY.xyz[1][2] ) - 
  528.              ( (double)POLY.xyz[0][1] * (double)POLY.xyz[2][2] ) );
  529.     } 
  530.     else {
  531.       area = 0.0;
  532.       for (c=0;c<POLY.vertices-1;++c)
  533.       area += ( (double)POLY.xyz[c][1]   * (double)POLY.xyz[c+1][2] ) - 
  534.           ( (double)POLY.xyz[c+1][1] * (double)POLY.xyz[c][2] );
  535.       area += ( (double)POLY.xyz[POLY.vertices-1][1] * (double)POLY.xyz[0][2] ) - 
  536.           ( (double)POLY.xyz[0][1] * (double)POLY.xyz[POLY.vertices-1][2] );
  537.       area /= 2.0;
  538.     }
  539.     if (area < 0.0) { 
  540.     ++yzcpoly; 
  541.     ++yzpols; 
  542.     for(c=0;c<POLY.vertices;++c) POLS.pols_points[c+1] =  POLY.index[c];
  543.     return(0);
  544.     }
  545.     if (area > 0.0) {
  546.     ++yzccpoly;
  547.     ++yzpols;
  548.     for(c=0,d=POLY.vertices;c<POLY.vertices;++c,--d) {
  549.         POLS.pols_points[d] = POLY.index[c];
  550.     }
  551.     return(0);
  552.     }
  553.     if (area == 0.0)  poly_area_xz();
  554.     return(0);
  555. }
  556. poly_area_xz()
  557. {
  558.     int c=0,d=0;
  559.     if (POLY.vertices == 3) {
  560.       area = 0.5 * ( ( (double)POLY.xyz[0][0] * (double)POLY.xyz[1][1] ) + 
  561.              ( (double)POLY.xyz[1][0] * (double)POLY.xyz[2][1] ) + 
  562.              ( (double)POLY.xyz[2][0] * (double)POLY.xyz[0][1] ) - 
  563.              ( (double)POLY.xyz[1][0] * (double)POLY.xyz[0][1] ) - 
  564.              ( (double)POLY.xyz[2][0] * (double)POLY.xyz[1][1] ) - 
  565.              ( (double)POLY.xyz[0][0] * (double)POLY.xyz[2][1] ) );
  566.     } 
  567.     else {
  568.       area = 0.0;
  569.       for (c=0;c<POLY.vertices-1;++c)
  570.       area += ( (double)POLY.xyz[c][0]   * (double)POLY.xyz[c+1][1] ) - 
  571.           ( (double)POLY.xyz[c+1][0] * (double)POLY.xyz[c][1] );
  572.       area += ( (double)POLY.xyz[POLY.vertices-1][0] * (double)POLY.xyz[0][1] ) - 
  573.           ( (double)POLY.xyz[0][0] * (double)POLY.xyz[POLY.vertices-1][1] );
  574.       area /= 2.0;
  575.     }
  576.     if (area < 0.0) { 
  577.     ++xzcpoly; 
  578.     ++xzpols; 
  579.     for(c=0;c<POLY.vertices;++c) POLS.pols_points[c+1] =  POLY.index[c];
  580.     return(0);
  581.     }
  582.     if (area > 0.0) {
  583.     ++xzccpoly;
  584.     ++xzpols;
  585.     for(c=0,d=POLY.vertices;c<POLY.vertices;++c,--d) {
  586.         POLS.pols_points[d] = POLY.index[c];
  587.     }
  588.     return(0);
  589.     }
  590.     if (area == 0.0) { 
  591.     ++nonpols;
  592.     for(c=0;c<POLY.vertices;++c) POLS.pols_points[c+1] =  POLY.index[c];
  593.     }
  594.     return(0);
  595. }
  596.